package org.iweb.html.engine.render;

import org.iweb.html.api.AbstractHtmlElement;
import org.iweb.ioc.Injectable;

@Injectable("IRenderEngine")
public class RenderEngine implements IRenderEngine{

	public String render(AbstractHtmlElement element){
		return renderRic("",element);
	}
	
	protected String renderRic(String result,AbstractHtmlElement element){
		try{
			
			if(element.getStyle()!=null)
				element.getAttributes().put("style", element.getAllStyle());
			
			element.onBeforeRender(result);	
			if(element.getChilds().size()==0){				
				return element.getStartHtml()+element.getEndHtml();				
			}
					
			result+=element.getStartHtml();
					
			for(String childId: element.getChilds().keySet()){
				result+=renderRic("", element.getChilds().get(childId));
			}
				
			return result+element.getEndHtml();
		}finally{
			element.onAfterRender(result);
		}
	}
	
}
